$(document).ready(function() {
  // 获取页面元素
  var myElement = document.getElementsByClassName('headroom')[0];
  // 创建 Headroom 对象，将页面元素传递进去
  if (!!myElement) {
    var headroom  = new Headroom(myElement, {
      tolerance: 5,
      offset : 205,
      classes: {
        initial: "animated",
        pinned: "slideDown",
        unpinned: "slideUp"
      }
    });
    // 初始化
    headroom.init(); 
  }
  //导航栏动效
  $('.dropdown.menu-item').on('mouseenter', function(){
    var e = $(this);
    e.addClass("enter");
  });
  $('.dropdown.menu-item').on('mouseleave', function(){
    $('.dropdown.menu-item').removeClass("enter");
  });

  if ($('.swiper-container').length > 0) {
    // JavaScript Document
    autoplay = true;
    resize = true;
    var mySwiper = new Swiper('.swiper-container', {
      autoplay: 4000,
      progress: true,
      speed: 1000,
      paginationClickable: true,
      loop: true,
      onProgressChange: function(swiper) {
        for (var i = 0; i < swiper.slides.length; i++) {
          var slide = swiper.slides[i];
          var progress = slide.progress;
          var translate;
          if (progress > 0) {
            translate = progress * 0.9 * swiper.width;
            scale = 1 - progress * 0.1;
            if (progress > 1) {
              scale = 0.9;
            }
            txtPositionX = 0;
            txtPositionY = progress * 30 + 'px';

          } else {
            translate = 0;
            scale = 1;
            txtPositionX = -progress * 1000 + 'px';
            txtPositionY = 0;
          }

          var txts = slide.querySelectorAll('.txt');
          for (var j = 0; j < txts.length; j++) {
            swiper.setTransform(txts[j], 'translate3d(' + txtPositionX + ',' + txtPositionY + ',0)');
          }
          swiper.setTransform(slide, 'translate3d(' + (translate) + 'px,0,0) scale(' + scale + ')');

        }
      },
      onTouchStart: function(swiper) {
        for (var i = 0; i < swiper.slides.length; i++) {
          swiper.setTransition(swiper.slides[i], 0);
          var txts = swiper.slides[i].querySelectorAll('.txt');
          for (var j = 0; j < txts.length; j++) {
            swiper.setTransition(txts[j], 0);
          }
        }
      },
      onSetWrapperTransition: function(swiper, speed) {
        for (var i = 0; i < swiper.slides.length; i++) {
          swiper.setTransition(swiper.slides[i], speed);
          var txts = swiper.slides[i].querySelectorAll('.txt');
          for (var j = 0; j < txts.length; j++) {
            swiper.setTransition(txts[j], speed);
          }
        }
      },
      onSlideChangeStart: function(swiper) {
        if (autoplay) {
          if (swiper.activeLoopIndex === 0) {
            $('.pagination li').removeClass('current');
            $('.pagination li').eq(0).removeClass('replace');
          }
          if (swiper.activeLoopIndex == 1) {
            $('.pagination li').eq(0).removeClass('firstCurrent current').addClass('replace');
          }
          $('.pagination li').eq(swiper.activeLoopIndex).addClass('current');
        } else {
          $('.pagination li').removeClass('current firstCurrent click');
          $('.pagination li').eq(swiper.activeLoopIndex).addClass('current');
        }

      },
      onFirstInit: function(swiper) {
        $('.pagination li').eq(0).addClass('firstCurrent');
      },

      onAutoplayStop: function() {
        autoplay = false;
        $('.autoplay').removeClass('autoplay');
      },
    });

    // Set Z-Indexes
    for (var i = 0; i < mySwiper.slides.length; i++) {
      mySwiper.slides[i].style.zIndex = i;
    }
    $(".swiper-container").animate({ opacity: 1 }, 'fast');

    $('.arrow-left').on('click', function(e) {
      e.preventDefault();
      mySwiper.swipePrev();
    });
    $('.arrow-right').on('click', function(e) {
      e.preventDefault();
      mySwiper.swipeNext();
    });
    $(".pagination li").on('touchstart mousedown', function(e) {
      e.preventDefault();
      if (autoplay) {
        autoplay = false;
        mySwiper.stopAutoplay();
        $(this).addClass('click');
      }

      mySwiper.swipeTo($(this).index());
    });
    window.onresize = function() {
      if (resize) {
        resize = false;
        setTimeout(function() {
          resize = true;
          mySwiper.reInit();
        }, 2000);
      }
    };
  }

  function autoPlayYouTubeModal() {
    var trigger = $("body").find('[data-toggle="modal"]');
    trigger.click(function() {
      var theModal = $(this).data("target"),
        videoSRC = $(this).attr("data-theVideo"),
        videoSRCauto = videoSRC + "?autoplay=1";
      $(theModal + ' iframe').attr('src', videoSRCauto);
      $(theModal + ' button.close').click(function() {
        $(theModal + ' iframe').attr('src', videoSRC);
      });
    });
  }

  if ($('#video_btn').length > 0) {
    $('#video_dlg').on('hidden.bs.modal', function() {
      $('#video_dlg iframe').removeAttr('src');
    });
    $(document).ready(function() {
      autoPlayYouTubeModal();
    });
  }

  if ($('.aura').length > 0) {
    console.log('init scroll reveal');
    window.sr = ScrollReveal({ reset: true });
    // sr.reveal('.text_panel');
    // sr.reveal('.text_panel2');
    // sr.reveal('.text_panel3');
    // sr.reveal('.text_panel4');
  }
});